#include "..\define.h"
#include "..\ApplicationManager.h"

#include "CloseManager.h"

CloseManager::CloseManager() : shut_donw_(false)
{
}

CloseManager::~CloseManager()
{
    input_manager_->destroyInputObject( key_board_);
    OIS::InputManager::destroyInputSystem(input_manager_);
    input_manager_ = nullptr;
}

//----------------------------------- static function
CloseManager& CloseManager::getInstance()
{
    static CloseManager _close;
    return _close;
}
//----------------------------------------------------------------- public
bool CloseManager::frameStarted(const Ogre::FrameEvent &evt)
{
    key_board_->capture();
    if( true == shut_donw_ )
    {
        return false;
    }
    return true;
}

void CloseManager::InitObject()
{
    Ogre::LogManager::getSingletonPtr()->logMessage("*** CloseManager ***");
    size_t windowHnd = 0;
    std::ostringstream windowHndStr;
    OIS::ParamList pl;
    _singleton_application.getWindow()->getCustomAttribute("WINDOW", &windowHnd);
    windowHndStr << windowHnd;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    input_manager_ = OIS::InputManager::createInputSystem(pl);
    key_board_ = static_cast<OIS::Keyboard*>(input_manager_->createInputObject
                                              (OIS::OISKeyboard, true));

    key_board_->setEventCallback(this);
    _singleton_application.getRoot()->addFrameListener(this);
}

bool CloseManager::keyPressed( const OIS::KeyEvent &evt )
{
    if( OIS::KC_ESCAPE == evt.key )
    {
        shut_donw_ = true;
        return false;
    }
    return true;
}

bool CloseManager::keyReleased( const OIS::KeyEvent &evt )
{
    return true;
}